#' @TODO 分组均值热图
#' @title 分组均值热图
#' @param Input 输入数据，行名是各个得分指标名称，列名是细胞名称
#' @param GroupDF 均值分组信息，行名为cell名称 ，第一组作为热图注释，细胞分类或者其他分类名称
#' @param w 宽
#' @param h 高
#' @param od 文件输出目录
#' @param FileName 文件生成名称
#' @param ColorUsed 分组信息注释所用颜色
#' @return a list
#' @export
#' @author *WYK*
Average_Heat <- function(Input = score_res, GroupDF = cell_group, w = 5, h = 7.5, od = file.path(out_home, "out/"),
                         FileName = "mean_score", ColorUsed = scales::hue_pal()(5), name_in_heatmap = NULL) {
    Input <- as.data.frame(Input) %>%
        rownames_to_column("Group") %>%
        pivot_longer(cols = -Group, names_to = "cell", values_to = "score")

    group_chr <- colnames(GroupDF)[1]

    GroupDF <- GroupDF %>%
        rownames_to_column("cell") %>%
        rename(celltype_new = 2)

    dat <- inner_join(Input, GroupDF) %>%
        group_by(celltype_new, Group) %>%
        mutate(score_mean = mean(score)) %>%
        select(-cell, -score) %>%
        distinct() %>%
        ungroup()

    dat <- dat %>%
        pivot_wider(names_from = celltype_new, values_from = score_mean) %>%
        as.data.frame() %>%
        column_to_rownames("Group")

    if (is.factor(GroupDF[[2]])) {
        index <- levels(GroupDF[[2]]) %>% as.character()
    } else {
        index <- sort(unique(GroupDF[[2]])) %>% as.character()
    }

    dat <- dat %>% dplyr::select(all_of(index))

    group_infor_inFun <- data.frame(sample = colnames(dat), group_chr = colnames(dat)) %>%
        column_to_rownames("sample")

    colnames(group_infor_inFun)[1] <- group_chr

    source("/Pub/Users/wangyk/Project_wangyk/Codelib_YK/some_scr/Heatmap_manul.R")
    a <- Heatmap_manul(
        data_input = dat, color_used = ColorUsed, group_infor = group_infor_inFun, Colored_OtherInfor = F,
        saveplot = T, output_dir = od,
        var_name = FileName, width_used = w, height_used = h, heatmap_name = " ",
        cluster_name = name_in_heatmap, show_rownames = T, DoWilcox.test = T, rownames_fontsize = 7
    )

    return(dat)
}
